package com.bytecodezz.app.framework.base.adapter

import android.content.Context
import android.util.SparseArray
import android.view.LayoutInflater
import android.view.View
import androidx.annotation.IdRes
import androidx.annotation.LayoutRes
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding

/**
 * @author: bytecodezz
 * @version: 1.0
 * @time: 2021/7/8 12:58
 * @package: com.bytecodezz.app.framework.base.adapter
 *
 * @description:
 *
 */
open class BaseViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    lateinit var binding: ViewBinding

    constructor(context: Context, @LayoutRes layoutId: Int) : this(
        LayoutInflater.from(context).inflate(layoutId, null)
    )

    constructor(viewBinding: ViewBinding) : this(viewBinding.root) {
        binding = viewBinding
    }

    fun <V : ViewBinding> getViewBinding(): V? {
        return binding as V
    }


    private var views: SparseArray<View> = SparseArray()

    fun <T : View?> getView(@IdRes viewId: Int): T? {
        var view = views!![viewId]
        if (view == null) {
            view = itemView.findViewById(viewId)
            views!!.put(viewId, view)
        }
        return view as T
    }
}